﻿using System;
using System.Windows.Forms;
using WoSoft.PACS.Data.Models;
using WoSoft.PACS.Data.User;
using System.IO;

namespace WoSoft.PACS.Win
{
    public partial class FrmLogin : Form
    {
        //当前登录用户
        public UserAccountModel LoginUser { get; private set; }

        public FrmLogin()
        {
            InitializeComponent();

            WoSoft.PACS.Utility.LogHelper.Info("======程序启动======");

            if (Directory.Exists(Application.StartupPath + @"\Temp\"))
            {
                Directory.Delete(Application.StartupPath + @"\Temp\",true);
            }

            if (!Directory.Exists(Application.StartupPath + @"\Temp\"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"\Temp\");
            }

        }

        //加载数据
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.tbUserAccount.Text = Properties.Settings.Default.DefaultUserName;
            if (Properties.Settings.Default.AutoLogin)
            {
                this.tbPwd.Text = Properties.Settings.Default.DefaultPassWord;
                buttonX1_Click(null, null);
            }
            this.ActiveControl = this.tbPwd;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.errorProvider1.Clear();
            if (CheckData())
            {
                var accoutData = new AccountData();
                if (accoutData.GetServerDate() != DateTime.Now.ToString("yyyy-MM-dd"))
                {
                    MessageBox.Show("本机日期和服务器日期不一致，请检查！", "提示");
                    return;
                }

                var r = accoutData.Login(this.tbUserAccount.Text.Trim(), this.tbPwd.Text.Trim());
                if (r != null)
                {
                    LoginUser = r;
                    //登录
                    Program.yymc = accoutData.GetXTCS("医院名称");
                    Program.yhmc = LoginUser.Name;
                    //默认值
                    Properties.Settings.Default.DefaultUserName = tbUserAccount.Text;
                    Properties.Settings.Default.Save();

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.errorProvider1.SetError(this.tbUserAccount, "用户名称或密码错误.");
                }
            }
        }

        private bool CheckData()
        {
            if (this.tbUserAccount.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbUserAccount, "请录入登录的用户帐户.");
                return false;
            }

            if (this.tbPwd.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbPwd, "请录入登录的用户密码.");
                return false;
            }


            return true;
        }


        private void lkClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Close();
        }

    }
}
